package com.cosytek.farmmanager.data;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by Happy on 2015/4/19.
 */
public class Farm {
    private static final long FARM_DATA_EXPIRED_TIMEOUT = 1000 * 60 * 10;
    private String id;
    private String name;
    private String description;

    private long refreshTime;

    private ArrayList<TaskSummary> taskSummaries;
    private ArrayList<Task> tasks;

    private int sensorCount;
    private int executorCount;

    private Farm() {

    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public ArrayList<TaskSummary> getTaskSummaries() {
        return taskSummaries;
    }

    public Task getTaskById(String taskId) {
        if (tasks == null) {
            return null;
        }

        for(Task t: tasks) {
            if (t.getId().equals(taskId)) {
                return t;
            }
        }

        return null;
    }

    public static Farm createFromJSONObject(JSONObject jsonObject) {
        Farm farm = new Farm();
        if (farm.refreshFromJSONObject(jsonObject)) {
            return farm;
        }

        return null;
    }

    public boolean refreshFromJSONObject(JSONObject jsonObject) {
        try {
            id = jsonObject.getString("id");
            name = jsonObject.getString("name");
            description = jsonObject.getString("description");

            if (taskSummaries == null) {
                taskSummaries = new ArrayList<TaskSummary>();
            }
            taskSummaries.clear();

            JSONArray ta = jsonObject.getJSONArray("tasks");
            for (int i = 0; i < ta.length(); i++) {
                JSONObject jt = ta.getJSONObject(i);

                TaskSummary ts = TaskSummary.createFromJSONObject(jt);
                if (ts != null) {
                    taskSummaries.add(ts);
                }
            }

            return true;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean isDataExpired() {
        Calendar c = Calendar.getInstance();
        long now = c.getTimeInMillis();

        if (now - refreshTime > FARM_DATA_EXPIRED_TIMEOUT) {
            return true;
        }

        return false;
    }

    public int getTaskCount() {
        if (taskSummaries == null) {
            return 0;
        }
        return taskSummaries.size();
    }

    public int getPlanCount() {
        return 0;
    }

    public int getSensorCount() {
        return sensorCount;
    }

    public int getExecutorCount() {
        return executorCount;
    }

    public TaskSummary getTaskSummary(int position) {
        if (taskSummaries == null) {
            return null;
        }

        return taskSummaries.get(position);
    }

    public Task onTaskLoaded(String respBody) {
        try {
            JSONObject jt = new JSONObject(respBody);
            String id = jt.getString("id");

            if (tasks == null) {
                tasks = new ArrayList<Task>();
            }

            for(Task t:tasks) {
                if (t.getId().equals(id)) {
                    t.refreshFromJSONObject(jt);
                    return t;
                }
            }

            Task t = Task.createFromJSONObject(jt);
            tasks.add(t);

            return t;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }
}
